//
//  KFDMeViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 6/8/15.
//  Copyright (c) 2015 kfd. All rights reserved.
//

#import "KFDMeViewController.h"
#import "KFDUIConfigurator.h"
#import "KFDAccountManager.h"
#import "KFDLoginViewController.h"
#import "KFDRegisterViewController.h"
#import "KFDMyActivitiesViewController.h"
#import "KFDAccountCenterViewController.h"
#import "KFDInjectionViewController.h"
#import "KFDWithdrawViewController.h"
#import "UIAlertController+KFD.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "KFDMessageListViewController.h"
#import "KFDChatListViewController.h"
#import "ui_config.h"

@interface KFDMeViewController ()

@property (strong,nonatomic) UIImageView* avatar;
@property (strong,nonatomic) KFDAccountManager* accountManager;
@property (assign,nonatomic) BOOL hide;
@property (strong,nonatomic) UILabel* nameLabel;

@end

@implementation KFDMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBar];
    self.title=@"我的";
    
    self.avatar=[UIImageView new];
    self.avatar.frame=CGRectMake(0, 0, 80, 80);
    self.avatar.center=CGPointMake(uiConfigurator.WIDTH/2, 100);
    self.avatar.layer.cornerRadius=80/2;
    self.avatar.layer.masksToBounds=YES;
    UITapGestureRecognizer* tapRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarPressed)];
    [self.avatar addGestureRecognizer:tapRecognizer];
    self.avatar.userInteractionEnabled=YES;
    [self.view addSubview:self.avatar];
    self.accountManager=[KFDAccountManager sharedManager];
    
    [self setupNameLabel];
    
    if (self.accountManager.injectionUrl&&self.accountManager.withdrawUrl) {
        self.hide=NO;
    }else{
        self.hide=YES;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accountInfoReady) name:KFDAccountInfoReadyNotificatoin object:nil];
}

-(void)setupNameLabel
{
    self.nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    self.nameLabel.center=CGPointMake(SCREEN_WIDTH/2, 150);
    self.nameLabel.textAlignment=NSTextAlignmentCenter;
    self.nameLabel.font=[UIFont systemFontOfSize:15];
    self.nameLabel.textColor=KFD_WHITE;
    [self.view addSubview:self.nameLabel];
}

-(void)accountInfoReady
{
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:self.accountManager.avatar] placeholderImage:[UIImage imageNamed:@"avatar"]];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.nameLabel.text=self.accountManager.nickname;
    });
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KFDAccountInfoReadyNotificatoin object:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:self.accountManager.avatar] placeholderImage:[UIImage imageNamed:@"avatar"]];
    self.nameLabel.text=self.accountManager.nickname;
}

-(void)avatarPressed
{
    KFDAccountManager* accountManager=[KFDAccountManager sharedManager];
    if ([accountManager isLogin]) {
        KFDAccountCenterViewController* accountCenterVC=[[UIStoryboard storyboardWithName:@"AccountCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"KFDAccountCenterViewController"];
        [self.navigationController pushViewController:accountCenterVC animated:YES];
    }else{
        UIViewController* loginVC=[[UIStoryboard storyboardWithName:@"KFDLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"KFDLoginViewController"];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return self.hide?0:2;
        case 1:
            return 2;
        case 2:
            return 1;
        default:
            return 0;
    }
}

#pragma mark - Table view delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KFDAccountManager* accountManager=[KFDAccountManager sharedManager];
    if ([accountManager isLogin]) {
        switch (indexPath.section) {
            case 0:
            {
                switch (indexPath.row) {
                    case 0:
                    {
                        UIViewController* injectionVC=[[KFDInjectionViewController alloc] init];
                        [self.navigationController pushViewController:injectionVC animated:YES];
                        break;
                    }
                    case 1:
                    {
                        UIViewController* withdrawVC=[[KFDWithdrawViewController alloc] init];
                        [self.navigationController pushViewController:withdrawVC animated:YES];
                        break;
                    }
                }
                break;
            }
            case 1:
            {
                switch (indexPath.row) {
                    case 0:
                    {
                        KFDMyActivitiesViewController* myEventsVC=[[KFDMyActivitiesViewController alloc] init];
                        [self.navigationController pushViewController:myEventsVC animated:YES];
                        break;
                    }
                    case 1:
                    {
                        UIViewController* messagesVC=[[KFDMessageListViewController alloc] init];
                        [self.navigationController pushViewController:messagesVC animated:YES];
                        break;
                    }
                }
                break;
            }
                
            case 2:
            {
                UIViewController* settingsVC=[[UIStoryboard storyboardWithName:@"Settings" bundle:nil] instantiateViewControllerWithIdentifier:@"KFDSettingsViewController"];
                [self.navigationController pushViewController:settingsVC animated:YES];
                break;
            }
            
        }
    }else{
        [UIAlertController presentLoginRegisterAlertControllerWithMessage:@"请您先登录" byViewController:self];
    }
}

@end
